/*!
 * Copyright (c) 2023-present StarDreamNet
 */
(document.loader || (document.loader = [])).push(["shequ", class {
    constructor() {
        this.page = 1;
        this.data = null;
        this.maxPage = 0;
        this.pa = new URL(location.href).searchParams.get("pa") || "all";
    };
    loadStart() {
        $(".app-cards").attr("load", "").html("").append($("<p class='load'>加载中</p>"));
        $(".app-main-pages").html("");
        this.resize();
    };
    loadEnd() {
        $(".app-cards").removeAttr("load", "").find("p.load").remove();
        this.resize();
    };
    loadError(err = "加载出错") {
        $(".app-cards").attr("load", "").attr("error", "").html(`<p>${err}</p>`);
        this.resize();
    };
    loadAll() {
        var th = this;
        th.loadStart();
        $.get('/api/shequ/get_post_list/' + this.page + '/' + this.pa + "?t=" + Date.now()).done(function (responseText) {
            var data = responseText;
            if (data.code == 200) {
                th.data = data.data;

                th.loadEnd();
                th.loadPosts();
                th.loadPages();
            } else {
                th.loadError(data.msg);
            }
        }).fail(function (error) {
            th.loadError();
        });
    };
    loadPosts() {
        var data = this.data.data;
        var cards = $('.app-cards');
        this.loadPostsByData(data, cards);
        this.resize();
    };
    loadPages() {
        $(".app-main-pages").html("");

        var page = this.page;
        var size = this.data.size;
        var omit = true;
        var th = this;

        var left = $(`<button class="left">&lt;</button>`)
        left.appendTo($(".app-main-pages"));
        for (let i = 0; i < size; i++) {
            let toadd = false;
            if (i === 0) toadd = true;
            if (i === size - 1) toadd = true;
            if (i - page < 2 && i - page > -4) toadd = true;
            if (page < 5 && i < 7) toadd = true;
            if (page > size - 5 && i > size - 7) toadd = true;
            if (toadd) {
                let button = $(`<button class="${(i + 1) == page ? `active` : ""} page-num">${i + 1}</button>`);
                button.on("click", function () {
                    th.goToTop();
                    th.change(Number(i + 1));
                })
                button.appendTo($(".app-main-pages"));
                omit = true;
            } else {
                if (omit) {
                    $(`<button disabled>...</button>`).appendTo($(".app-main-pages"));
                    omit = false;
                }
            }
        }
        var right = $(`<button class="right">&gt;</button>`)
        right.appendTo($(".app-main-pages"));

        left.prop("disabled", page === 1);
        right.prop("disabled", page === size)

        left.on("click", () => {
            this.change(this.getPage() - 1);
        })

        right.on("click", () => {
            this.change(this.getPage() + 1);
        })

        this.resize();
    };
    goToTop() {
        window.scrollTo({
            top: 0
        })
    };
    change(page) {
        this.setPage(page);
        this.loadAll();
    }
    setPage(page) {
        this.page = Number(page) || 1;
        return this.page;
    };
    getPage() {
        return this.page;
    };
    loadPartition() {
        var xl = $("<p>加载中</p>");
        xl.appendTo(".app-shequ-partition");
        $.get('/api/shequ/get_post_partition/?t=' + Date.now()).done((responseText) => {
            xl.remove();
            var data = responseText;
            if (data.code == 200) {
                var d = [{
                    id: "all",
                    name: "全部帖子"
                }].concat(data.data);

                for (let va of d) {
                    let el = $("<a></a>");
                    el.text(va.name);
                    el.attr("href", "?pa=" + va.id);
                    if (this.pa == String(va.id)) el.addClass("active")
                    $(".app-shequ-partition").append(el);
                }
            } else {
                $(".app-shequ-partition").append(`<p>${data.msg}</p>`);
            }
        }).fail(function (error) {
            $(".app-shequ-partition").append("<p>加载出错</p>");
        });
    };
    partitionPosition() {
        var par = $(".app-shequ-partition");
        if ($(window).scrollTop() >= 50) par.addClass("fixed");
        else par.removeClass("fixed");
        requestAnimationFrame(this.partitionPosition.bind(this));
    };
    init_(require) {
        const { resize, dateSpanString } = require("index");
        const { loadPostsByData } = require("postlist");

        this.resize = resize;
        this.dateSpanString = dateSpanString;
        this.loadPostsByData = loadPostsByData;

        this.setPage(1);
        this.loadAll();
        this.loadPartition();
        this.partitionPosition();
    };
}])